public class Caballo
{
    public static final int FILAS = 8;
    public static final int COLUMNAS = 8;
    private int tabla[][] = new int[FILAS][COLUMNAS];
    
    private int desplaz_fil[] = new int[FILAS];
    private int desplaz_col[] = new int[COLUMNAS];
    
    private int ind;
    
    public Caballo () {
        int i = 0;
        int j = 0;
        desplaz_fil[0] = 1;
        desplaz_fil[1] = 2;
        desplaz_fil[2] = -1;
        desplaz_fil[3] = -2;
        desplaz_fil[4] = 1;
        desplaz_fil[5] = 2;
        desplaz_fil[6] = -1;
        desplaz_fil[7] = -2;
        desplaz_col[0] = 2;
        desplaz_col[1] = 1;
        desplaz_col[2] = -2;
        desplaz_col[3] = -1;
        desplaz_col[4] = -2;
        desplaz_col[5] = -1;
        desplaz_col[6] = 2;
        desplaz_col[7] = 1;
        for (i = 0; i < FILAS; i++) {
            for (j = 0; j < COLUMNAS; j++) {
                tabla[i][j] = 0;
            }
        }
        ind = 2;
    }
    
    public void procesar (int x, int y) {
        int i = 0;
        int j = 0;
        int cuentasaltos = 0;
        int fil = 0;
        int col = 0;
        int k = 0;
        int cuento = 0;
        int dondevoy = 0;
        int nuevafil = 0;
        int nuevacol = 0;
        
        tabla[x][y] = 1;
        fil = x;
        col = y;
        
        for (j = 2; j <= (FILAS*COLUMNAS); j++) {
            cuentasaltos = 9;
            for (i = 0; i < FILAS; i++) {
                nuevafil = fil + desplaz_fil[i];
                nuevacol = col + desplaz_col[i];
                if (nuevafil >= 0 && nuevafil <= 7 && nuevacol >= 0 && nuevacol <= 7 && tabla[nuevafil][nuevacol] == 0){
                    cuento = 0;				
                    for (k = 0; k < 8; k++) {
                        if ((nuevafil + desplaz_fil[k] >= 0 && nuevafil + desplaz_fil[k] <= 7) && (nuevacol + desplaz_col[k] >= 0 && nuevacol + desplaz_col[k] <= 7) && (tabla[nuevafil+desplaz_fil[k]][nuevacol+desplaz_col[k]] == 0)) {
                            cuento++;
                        }
                    }
                    
                    if (cuento < cuentasaltos) {
                        cuentasaltos = cuento;
                        dondevoy = i;
                    }
                }
            }
            
            fil = fil + desplaz_fil[dondevoy];
            col = col + desplaz_col[dondevoy];
            tabla[fil][col] = j;
        }
    }
    
    public void escribir () {
        int i = 0;
        int j = 0;
        for (i = 0; i < FILAS; i++) {
            System.out.println(" -----------------------------------------");
            for (j = 0; j < COLUMNAS; j++) {
                if (tabla[i][j] > 9) {
                    System.out.print(" | " + tabla[i][j]);
                }
                else {
                    System.out.print(" | " + tabla[i][j] + " ");
                }
            }
            System.out.println(" |");
        }
        System.out.println(" -----------------------------------------");
    }
    
    public int getNum (int i, int x) {
        return tabla[i][x];
    }
    
    public int[] nextNum () {
        int v[] = new int[3];
                
        int i = 0;
        int j = 0;
        for (i = 0; i < FILAS; i++) {
            for (j = 0; j < COLUMNAS; j++) {
                if (ind == tabla[i][j]) {
                    v[0] = i;
                    v[1] = j;
                    v[2] = ind;
                    ind++;
                    return v;
                }
            }
        }
        return v;
    }
}